function Basis = BasisConstruct(X)

%%inputs:
%   X: X to construct basis
%%


    N   =   size(X,1);
    
    DV = X(:,4:end); % dummy variables

    % 2. The computation of the sieve basis weights when dim(X) = 1.
    % 2.1 For IPW with 2-dimensional X
    % we consider the following basis
    Basis       =   {};

    knots1      =   quantile(X(:,1),.5);
    knots2      =   quantile(X(:,2),.5);
    knots3      =   quantile(X(:,3),.5);
    
    Basis{1}    =   [ones(N,1), X(:,1).*X(:,2), X(:,2).*X(:,3), X(:,1).*X(:,3),... 
                            max(repmat(X(:,1),[1,size(knots1,2)])-repmat(knots1,[N,1]),0), ...
                            max(repmat(X(:,2),[1,size(knots2,2)])-repmat(knots2,[N,1]),0), ...
                            max(repmat(X(:,3),[1,size(knots3,2)])-repmat(knots3,[N,1]),0), ...
                            DV];
        
    Basis{2}    =   [ones(N,1), X(:,1).*X(:,2), X(:,2).*X(:,3), X(:,1).*X(:,3), ...
                            max(repmat(X(:,1),[1,size(knots1,2)])-repmat(knots1,[N,1]),0).^2, ...
                            max(repmat(X(:,2),[1,size(knots2,2)])-repmat(knots2,[N,1]),0).^2, ...
                            max(repmat(X(:,3),[1,size(knots3,2)])-repmat(knots3,[N,1]),0).^2, ...
                            DV]; 
                   
    Basis{3}    =   [ones(N,1), X(:,1), X(:,2), X(:,3), X(:,1).*X(:,2), X(:,2).*X(:,3), X(:,1).*X(:,3),... 
                                max(repmat(X(:,1),[1,size(knots1,2)])-repmat(knots1,[N,1]),0), ...
                                max(repmat(X(:,2),[1,size(knots2,2)])-repmat(knots2,[N,1]),0), ...
                                max(repmat(X(:,3),[1,size(knots3,2)])-repmat(knots3,[N,1]),0), ...
                                DV];

    Basis{4}    =   [ones(N,1), X(:,1), X(:,2), X(:,3), X(:,1).*X(:,2), X(:,2).*X(:,3), X(:,1).*X(:,3), ...
                                max(repmat(X(:,1),[1,size(knots1,2)])-repmat(knots1,[N,1]),0).^2, ...
                                max(repmat(X(:,2),[1,size(knots2,2)])-repmat(knots2,[N,1]),0).^2, ...
                                max(repmat(X(:,3),[1,size(knots3,2)])-repmat(knots3,[N,1]),0).^2, ...
                                DV]; 

    knots1 = quantile(X(:,1),[0.3,0.5]); 
    knots2 = quantile(X(:,2),[0.3,0.5]);  
    knots3 = quantile(X(:,3),[0.3,0.5]);
    
    Basis{5}    =   [ones(N,1), X(:,1).*X(:,2), X(:,2).*X(:,3), X(:,1).*X(:,3),... 
                            max(repmat(X(:,1),[1,size(knots1,2)])-repmat(knots1,[N,1]),0), ...
                            max(repmat(X(:,2),[1,size(knots2,2)])-repmat(knots2,[N,1]),0), ...
                            max(repmat(X(:,3),[1,size(knots3,2)])-repmat(knots3,[N,1]),0), ...
                            DV];
        
    Basis{6}    =   [ones(N,1), X(:,1).*X(:,2), X(:,2).*X(:,3), X(:,1).*X(:,3), ...
                            max(repmat(X(:,1),[1,size(knots1,2)])-repmat(knots1,[N,1]),0).^2, ...
                            max(repmat(X(:,2),[1,size(knots2,2)])-repmat(knots2,[N,1]),0).^2, ...
                            max(repmat(X(:,3),[1,size(knots3,2)])-repmat(knots3,[N,1]),0).^2, ...
                            DV]; 
                   
    Basis{7}    =   [ones(N,1), X(:,1), X(:,2), X(:,3), X(:,1).*X(:,2), X(:,2).*X(:,3), X(:,1).*X(:,3),... 
                                max(repmat(X(:,1),[1,size(knots1,2)])-repmat(knots1,[N,1]),0), ...
                                max(repmat(X(:,2),[1,size(knots2,2)])-repmat(knots2,[N,1]),0), ...
                                max(repmat(X(:,3),[1,size(knots3,2)])-repmat(knots3,[N,1]),0), ...
                                DV];

    Basis{8}    =   [ones(N,1), X(:,1), X(:,2), X(:,3), X(:,1).*X(:,2), X(:,2).*X(:,3), X(:,1).*X(:,3), ...
                                max(repmat(X(:,1),[1,size(knots1,2)])-repmat(knots1,[N,1]),0).^2, ...
                                max(repmat(X(:,2),[1,size(knots2,2)])-repmat(knots2,[N,1]),0).^2, ...
                                max(repmat(X(:,3),[1,size(knots3,2)])-repmat(knots3,[N,1]),0).^2, ...
                                DV];              
                                                                     
end